package com.example.glxt1.controller;



import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.glxt1.entity.Commodity;
import com.example.glxt1.service.ICommodityService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fumeichuan
 * @since 2023-01-16
 */
@RestController
@RequestMapping("/commodity")
public class CommodityController {
    @Resource
    private ICommodityService commodityService;

    @PostMapping("/save")
    public boolean save(@RequestBody Commodity commodity) {
        if(commodity.getCommodityId()==null){
            long l = System.currentTimeMillis();
            commodity.setCommodityId(l);
        }
        return commodityService.saveOrUpdate(commodity);
    }

    @PostMapping("/delete/batch")
    public boolean deleteBatch(@RequestBody List<Integer> ids) {
        return commodityService.removeByIds(ids);
    }

    @DeleteMapping("/delete/{id}")
    public boolean delete(@PathVariable Integer id) {
        return commodityService.removeById(id);
    }

    @GetMapping("/list")
    public List<Commodity> findAll() {
        return commodityService.list();
    }

    @GetMapping("/findOne/{id}")
    public Commodity findOne(@PathVariable Integer id) {
        return commodityService.getById(id);
    }

    @GetMapping("/page")
    public Page<Commodity> findPage(@RequestParam Integer pageNum,
    @RequestParam Integer pageSize) {
        return commodityService.page(new Page<>(pageNum, pageSize));
    }

}

